package com.Data;


import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import com.Managers.UserManagerClass;
import com.SoapClasses.SoapComments;
import com.shareup.R;
import com.utils.AlertDialogHelper;


public class Comment extends AsyncTask<String, String, Comment[]> {

    public static final int ACTION_ADD = 0;

    public static final int ACTION_DELETE = 1;

    public static final int ACTION_GET = 2;

    private int Action;

    private onAddCommentsCompleted callbackAdd;

    private onDeleteComentsCompleted callbackDelete;

    private onGetCommentsCompleted callbackGet;

    private Context context;

    private int IDComment;

    private int Last;

    private String Message;

    private Post Picture;

    private AlertDialog progress;

    public interface onAddCommentsCompleted {

        void Completed(String result);
    }

    public interface onDeleteComentsCompleted {

        void Completed(String result);
    }

    public interface onGetCommentsCompleted {

        void Completed(Comment[] result);
    }

    public Comment(Context context) {

        this.context = context;
    }

    public Comment(Context context, Post picture) {

        this.Picture = picture;
        this.context = context;

    }

    // ////////////////////
    @Override
    public Comment[] doInBackground(String... param) {

        // TODO Auto-generated method stub

        SoapComments sc = new SoapComments();
        Comment[] cm = new Comment[1];
        String mess = "";
        switch (Action) {
            case ACTION_ADD:
                String[] data = new String[4];
                data[0] = String.valueOf(this.Picture.getID());
                data[1] = String.valueOf(this.Picture.getUser().getID());
                data[2] = this.Message;
                data[3] = String.valueOf(UserManagerClass.UserLogged.getID());
                mess = sc.addComment(data);
                cm[0] = new Comment(null);
                cm[0].setMessage(mess);
                return cm;
            case ACTION_DELETE:
                cm = new Comment[1];
                mess = sc.deleteComment(this.IDComment);
                cm[0] = new Comment(null);
                cm[0].setMessage(mess);
                return cm;
            case ACTION_GET:
                return sc.getComments(this.Picture.getID(), Last);
            default:
                return null;
        }

    }

    public int getAction() {

        return Action;
    }

    public onAddCommentsCompleted getCallbackAdd() {

        return callbackAdd;
    }

    public onDeleteComentsCompleted getCallbackDelete() {

        return callbackDelete;
    }

    public onGetCommentsCompleted getCallbackGet() {

        return callbackGet;
    }

    public int getIDComment() {

        return IDComment;
    }

    public int getLast() {

        return Last;
    }

    public String getMessage() {

        return Message;
    }

    public Post getPicture() {

        return Picture;
    }

    @Override
    public void onPostExecute(Comment[] result) {

        if (callbackAdd != null) {
            callbackAdd.Completed(result[0].getMessage());
            progress.dismiss();
        }

        if (callbackDelete != null) {
            callbackDelete.Completed(result[0].getMessage());
            progress.dismiss();
        }

        if (callbackGet != null) {
            callbackGet.Completed(result);
        }

    }

    public void setAction(int action) {

        Action = action;
    }

    public void setCallbackAdd(onAddCommentsCompleted callbackAdd) {

        this.callbackAdd = callbackAdd;
    }

    public void setCallbackDelete(onDeleteComentsCompleted callbackDelete) {

        this.callbackDelete = callbackDelete;
    }

    public void setCallbackGet(onGetCommentsCompleted callbackGet) {

        this.callbackGet = callbackGet;
    }

    public void setIDComment(int iDComment) {

        IDComment = iDComment;
    }

    public void setLast(int last) {

        Last = last;
    }

    public void setMessage(String message) {

        Message = message;
    }

    public void setPicture(Post picture) {

        Picture = picture;
    }

    @Override
    protected void onPreExecute() {

        if (callbackAdd != null) {
            progress = AlertDialogHelper.setProgressDialog(progress, context.getResources().getString(R.string.dialog_commenting), context);
            progress.show();
        }
        if (callbackDelete != null) {
            progress = AlertDialogHelper.setProgressDialog(progress, context.getResources().getString(R.string.dialog_deleting), context);
            progress.show();
        }

    }

}
